package com.yhh.myUtils;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.*;

public class myStringUtils {
    public static String getRandomString(int length) {
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(62);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }

    public static List<String> getTimeUnit(String regular){
        int index = 0;
        for (int i = 0; i < regular.length(); i++) {
            char c = regular.charAt(i);
            if (Character.isDigit(c)) {
                if (i == regular.length() - 1) {
                    index = i + 1;
                } else {
                    index = i;
                }
            } else {
                index = i;
                break;
            }
        }
        return Arrays.asList(regular.substring(0, index),regular.substring(index));
    }
    public static String getCode(){
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");
        Base64.Encoder encoder = Base64.getEncoder();
        String encoderStr = null;
        encoderStr = encoder.encodeToString(formatter.format(new Date(System.currentTimeMillis())).getBytes(StandardCharsets.UTF_8));
        return encoderStr;
    }
    public static boolean decode(String code){
        Base64.Decoder decoder = Base64.getDecoder();
        String decoderStr = new String(decoder.decode(code), StandardCharsets.UTF_8);
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");
        return decoderStr.equals(formatter.format(new Date(System.currentTimeMillis())));
    }
}
